package Pre.Work;

import java.util.*;
import java.util.stream.Stream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Exam03 {
    //薪资最低的员⼯
    //有一个员工类Employee,里面有name、age和salary字段。请通过 Stream 对多个Employee对象按salary排序,
    //然后取前3个输出。
    public static void main(String[] args) {
        Employee e1 = new Employee("张三",18,9900);
        Employee e2= new Employee("李四",22,5500);
        Employee e3= new Employee("王五",21,3000);
        Employee e4 = new Employee("赵六",25,4500);
        Employee e5 = new Employee("周三狗",36,3600);
        Employee e6 = new Employee("傻子",26,12000);
        Employee e7 = new Employee("疯子",32,7800);
        Employee e8 = new Employee("瘸子",24,6600);
        Employee e9 = new Employee("小保洁",15,10500);

        HashMap<Integer,Employee> hashMap = new HashMap<>();
        hashMap.put(1,e1);
        hashMap.put(2,e2);
        hashMap.put(3,e3);
        hashMap.put(4,e4);
        hashMap.put(5,e5);
        hashMap.put(6,e6);
        hashMap.put(7,e7);
        hashMap.put(8,e8);
        hashMap.put(9,e9);

        Stream<Map.Entry<Integer, Employee>> stream = hashMap.entrySet().stream().sorted(new Comparator<Map.Entry<Integer, Employee>>() {
            @Override
            public int compare(Map.Entry<Integer, Employee> o1, Map.Entry<Integer, Employee> o2) {
                return (int) (o2.getValue().getSalary() - o1.getValue().getSalary());
            }
        });
        Stream limit = stream.limit(3);
        limit.forEach(System.out::println);


        //=====================================
        System.out.println();
        List<Employee> employees = new ArrayList<>();
        Employee zs = new Employee("张三", 23, 8000.5);
        Employee ls = new Employee("李四", 24, 8765);
        Employee ww = new Employee("王五", 22, 7638.5);
        Employee sl = new Employee("孙六", 21, 9000.5);
        Collections.addAll(employees, zs, ls, ww, sl);
        // 升序
        employees.stream().sorted((e, employee) -> Double.compare( employee.getSalary(), e.getSalary())).limit(3).forEach(e -> System.out.println(e));

    }
}
class Employee{
    private String name;
    private int age;
    private double salary;

    public Employee(String name, int age, double salary) {
        this.name = name;
        this.age = age;
        this.salary = salary;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public double getSalary() {
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }

    @Override
    public String toString() {
        return "Employee{" +
                "姓名：" + name + '\'' +
                ", 年龄：" + age +
                ", 工资：" + salary +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Employee employee = (Employee) o;
        return age == employee.age && Double.compare(salary, employee.salary) == 0 && Objects.equals(name, employee.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age, salary);
    }
}
